package com.gitee.faster.redis.protocol;

import lombok.Data;
import lombok.NonNull;
import lombok.experimental.Accessors;
import redis.clients.jedis.args.Rawable;
import redis.clients.jedis.args.RawableFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author hongda.li
 */
@Data
@Accessors(chain = true)
public class RedisResponse implements Iterable<Rawable> {

    private final Long timestamp = System.currentTimeMillis();

    private final List<Rawable> itemList = new ArrayList<>();

    public static RedisResponse of(String item) {
        return new RedisResponse().addItem(item);
    }

    public RedisResponse addItem(String item) {
        itemList.add(RawableFactory.from(item));
        return this;
    }

    public void addItems(RedisResponse response) {
        this.itemList.addAll(response.itemList);
    }

    public int size() {
        return this.itemList.size();
    }

    @Override
    @NonNull
    public Iterator<Rawable> iterator() {
        return this.itemList.iterator();
    }
}
